use core::panic::PanicInfo;

#[cfg(feature = "base_app")]
use cfw_kit::ota::Error as OtaErr;

#[cfg(feature = "base_app")]
use proto_bytes::frame::ToEc;

#[cfg(feature = "base_app")]
use utils::ufmt;



#[panic_handler]
fn on_panic(_info: &PanicInfo) -> ! {
    loop {}
}

#[cfg(feature = "base_app")]
impl From<OtaErr> for AppErr {
    fn from(value: OtaErr) -> Self {
        Self::Ota(value)
    }
}

#[cfg(feature = "base_app")]
#[derive(Clone, Copy, ufmt::derive::uDebug)]
pub enum AppErr {
    Ota(OtaErr),
    InvalidCmd,
    Invalid,
    Parse,

    #[cfg(feature = "cp_cargo")]
    Cargo(crate::cp_cargo::CargoErr),

    #[cfg(feature = "cp_drop")]
    Drop(crate::cp_drop::DropErr),

    #[cfg(feature = "cp_heat")]
    Heat(crate::cp_heat::HeatErr),

    #[cfg(feature = "cp_door")]
    Door(crate::cp_door::DoorErr),
}

#[cfg(feature = "base_app")]
impl ToEc for AppErr {

    fn to_ec(self) -> u8 {
        match self {
            Self::Ota(OtaErr::Flash(_)) => 1,
            Self::Ota(OtaErr::Id) => 2,
            Self::Ota(OtaErr::Md5) => 3,
            Self::Ota(OtaErr::Size) => 4,
            Self::InvalidCmd => 5,
            Self::Parse => 6,
            Self::Invalid => 7,

            #[cfg(feature = "cp_cargo")]
            Self::Cargo(e) => e.to_ec(),

            #[cfg(feature = "cp_drop")]
            Self::Drop(e) => e.to_ec(),

            #[cfg(feature = "cp_heat")]
            Self::Heat(e) => e.to_ec(),

            #[cfg(feature = "cp_door")]
            Self::Door(e) => e.to_ec(),
        }
    }
}



